package com.niodata.dp.core.resource;

public abstract class AbstractResource implements Resource {

  protected final ResourceType type;
  protected final String content;

  public AbstractResource(ResourceType type, String content) {
    this.type = type;
    this.content = content;
  }

  @Override
  public ResourceType resourceType() {
    return type;
  }

  @Override
  public String content() {
    return content;
  }

  @Override
  public String format() {
    return type.formatPrefix() + content;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((content == null) ? 0 : content.hashCode());
    result = prime * result + ((type == null) ? 0 : type.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    AbstractResource other = (AbstractResource) obj;
    if (content == null) {
      if (other.content != null) {
        return false;
      }
    } else if (!content.equals(other.content)) {
      return false;
    }
    return type == other.type;
  }

  @Override
  public String toString() {
    return "Resource [type=" + type + ", content=" + content + "]";
  }
}
